import { defineStore } from 'pinia'
import { ElMessage } from 'element-plus'
import $axios from '../tools/axios'

const basepath = '/ticket'
export const useTicketStore = defineStore('ticket', {
  state: () => {
    return {
      newticket: {
        trainno: '',
        trainPath: '',
        carriage: '',
        rows: '',
        seat: '',
        date: ''
      },
      ticket: {
        carriage: '',
        trainno: '',
        date: ''
      },

      fromnm: '',
      tonm: '',
      fromtime: '',
      totime: '',
      passenger: '',
      money: 0,

      orders: {
        trainno: '',
        fromnm: '',
        tonm: '',
        fromtime: '',
        totime: '',
        paynm: '',
        passnm: '',
        date: '',
        money: 0,
        carriage: '',
        rows: '',
        seat: '',
        paytime: '',
        state: 0
      },
      //
    
    }
  },

  actions: {
   

    // 查询座位
    async querySeat() {
      let name = sessionStorage.getItem('name')

      const path = basepath + '/queryseat'

      const { data: res } = await $axios.post(path, this.ticket)

      if (res.code === 200) {
        console.log(res.data)

        this.orders.trainno = res.data.trainno
        this.orders.fromnm = this.fromnm
        this.orders.tonm = this.tonm
        this.orders.fromtime = this.fromtime
        this.orders.totime = this.totime
        this.orders.paynm = name
        this.orders.passnm = this.passenger
        this.orders.date = '2023-' + this.ticket.date
        this.orders.money = parseFloat(this.money.replace(/[^0-9.]/g, ''))
        this.orders.carriage = res.data.carriage
        this.orders.rows = res.data.rows
        this.orders.seat = res.data.seat
        // this.orders.paytime = this.getCurrentTime()
        this.orders.state = 0

        //添加订单
        const addOrdersPath = '/orders/add'
        const { data: ans } = await $axios.post(addOrdersPath, this.orders)

        if (res.code === 200) {
          console.log('添加订单成功')
          console.log(ans.data)
        }

        let stationArray = res.data.trainPath.split('，')

        const index1 = stationArray.indexOf(this.fromnm)
        const index2 = stationArray.indexOf(this.tonm)

        const array1 = stationArray.slice(0, index1 + 1)
        const array2 = stationArray.slice(index2, stationArray.length)

        if (array1.length != 1) {
          const path1 = basepath + '/insertseat'

          this.newticket.trainno = res.data.trainno
          this.newticket.carriage = res.data.carriage
          this.newticket.rows = res.data.rows
          this.newticket.seat = res.data.seat
          this.newticket.date = res.data.date
          this.newticket.trainPath = array1.join('，')

          const { data: res1 } = await $axios.post(path1, this.newticket)

          if (res.code === 200) {
            console.log(res1.data)
          }
        }

        if (array2.length != 1) {
          const path2 = basepath + '/insertseat'

          this.newticket.trainno = res.data.trainno
          this.newticket.carriage = res.data.carriage
          this.newticket.rows = res.data.rows
          this.newticket.seat = res.data.seat
          this.newticket.date = res.data.date
          this.newticket.trainPath = array2.join('，')

          const { data: res2 } = await $axios.post(path2, this.newticket)

          if (res.code === 200) {
            console.log(res2.data)
          }
        }

        const delpath = basepath + '/delseat'

        const { data: delres } = await $axios.post(delpath, res.data)

        if (delres.code === 200) {
          console.log('删除座位成功')
        }
      } else {
        ElMessage.error('查询座位失败！')
      }
    }
  }
})
